package com.example.demo.module.develop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.service.impl.BaseServiceImpl;
import com.example.demo.entity.pojo.develop.DevelopBomMaterielPoJo;
import com.example.demo.entity.pojo.develop.DevelopBomPoJo;
import com.example.demo.module.develop.mapper.DevelopBomMapper;
import com.example.demo.module.develop.mapper.DevelopBomMaterielMapper;
import com.example.demo.module.develop.service.DevelopBomService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@Service("developBomService")
public class DevelopBomServiceImpl extends BaseServiceImpl<DevelopBomMapper, DevelopBomPoJo> implements DevelopBomService {
    @Resource
    private DevelopBomMapper mapper;
    @Resource
    private DevelopBomMaterielMapper materielMapper;

    private LambdaQueryWrapper<DevelopBomPoJo> getLambda(DevelopBomPoJo poJo) {
        Long id = poJo.getId();
        Long companyId = poJo.getCompanyId();
        Date createTime = poJo.getCreateTime();
        Date updateTime = poJo.getUpdateTime();
        Boolean isDelete = poJo.getIsDelete();
        String companyName = poJo.getCompanyName();
        String name = poJo.getName();
        String spec = poJo.getSpec();
        String code = poJo.getCode();
        Long createId = poJo.getCreateId();
        String createName = poJo.getCreateName();
        Boolean deleteFlag = poJo.getDeleteFlag();
        Date deleteTime = poJo.getDeleteTime();


        LambdaQueryWrapper<DevelopBomPoJo> wrapper = new LambdaQueryWrapper<DevelopBomPoJo>();
        wrapper
                .eq(id != null, DevelopBomPoJo::getId, poJo.getId())
                .eq(companyId != null, DevelopBomPoJo::getCompanyId, poJo.getCompanyId())
                .eq(createTime != null, DevelopBomPoJo::getCreateTime, poJo.getCreateTime())
                .eq(updateTime != null, DevelopBomPoJo::getUpdateTime, poJo.getUpdateTime())
                .eq(isDelete != null, DevelopBomPoJo::getIsDelete, poJo.getIsDelete())
                .eq(companyName != null, DevelopBomPoJo::getCompanyName, poJo.getCompanyName())
                .like(name != null, DevelopBomPoJo::getName, poJo.getName())
                .eq(spec != null, DevelopBomPoJo::getSpec, poJo.getSpec())
                .eq(code != null, DevelopBomPoJo::getCode, poJo.getCode())
                .eq(createId != null, DevelopBomPoJo::getCreateId, poJo.getCreateId())
                .eq(createName != null, DevelopBomPoJo::getCreateName, poJo.getCreateName())
                .eq(deleteFlag != null, DevelopBomPoJo::getDeleteFlag, poJo.getDeleteFlag())
                .eq(deleteTime != null, DevelopBomPoJo::getDeleteTime, poJo.getDeleteTime());
        return wrapper;
    }

    @Override
    public List<DevelopBomPoJo> findList(DevelopBomPoJo poJo) {
        return mapper.selectList(getLambda(poJo));
    }

    @Override
    public void update(DevelopBomPoJo poJo) {
        mapper.updateById(poJo);
    }

    @Override
    public void remove(List<Long> ids) {
        if (ids.size() != 0) {
            DevelopBomPoJo poJo = new DevelopBomPoJo();
            poJo.setDeleteTime(new Date());
            poJo.setDeleteFlag(true);
            ids.stream().forEach(id -> {
                poJo.setId(id);
                //删除BOM表
                mapper.updateById(poJo);
            });
        }
    }

    @Override
    public Page<DevelopBomPoJo> findPage(Integer page, Integer size, DevelopBomPoJo poJo) {
        return mapper.selectPage(new Page<>(page, size), getLambda(poJo));
    }

    //彻底删除
    @Override
    public void batchRemove(List<Long> ids) {
        if (ids.size() != 0) {
            //彻底删除bom表
            mapper.deleteBatchIds(ids);

            LambdaUpdateWrapper<DevelopBomMaterielPoJo> wrapper = new LambdaUpdateWrapper<DevelopBomMaterielPoJo>();
            //彻底删除对应的bom物料表
            ids.stream().forEach(id -> {
                wrapper
                        .eq(id != null, DevelopBomMaterielPoJo::getBomId, id);
                materielMapper.delete(wrapper);
            });

        }
    }


}